//
//  DSDownloadManager.m
//  dingDong
//
//  Created by admin on 16/6/23.
//  Copyright © 2016年 LB. All rights reserved.
//

#import "DSDownloadManager.h"
#import "AFNetworking.h"

@implementation DSDownloadManager

+ (DSDownloadManager *)sharedManager
{
    static DSDownloadManager *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}
//请求
+ (void)GetRequest:(NSString *)url
        parameters:(NSDictionary *)parameters
           success:(void (^)(id responseObject,int code , NSString *message ))success
           failure:(void (^)(NSError *error))failure
{
    
    
    //1.得到管理器
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 30.f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    //2.发送GET请求
    [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //如果有这个block 也就是有这个参数，防止nil
        if (success) {
            
            success(responseObject,
                    [responseObject[@"status"] intValue],
                    responseObject[@"message"]);
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);

        if (failure) {
            if(error.code == -1001)
            {
                [NSObject showAlertMessage:@"网络请求超时"];

            }else if (error.code == -1009)
            {
                [NSObject showAlertMessage:@"网络请求超时"];

            }else
            {
                [NSObject showAlertMessage:@"服务器维护中,请稍后再试"];
            }
            failure(error);
        }
    }];
    
    
}

+ (void)PostRequest:(NSString *)url
         parameters:(NSDictionary *)parameters
            success:(void (^)(id responseObject,int code , NSString *message ))success
            failure:(void (^)(NSError *error))failure
{
    //1.得到管理器
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFJSONRequestSerializer serializer];
    manager.responseSerializer=[AFJSONResponseSerializer serializer];
    
//    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
//    manager.requestSerializer.timeoutInterval = 10.f;
//    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    //2.发送GET请求
    [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //如果有这个block 也就是有这个参数，防止nil
        if (success) {

            success(responseObject,
                    [responseObject[@"status"] intValue],
                    responseObject[@"message"]);
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (failure) {
            if(error.code == -1001)
            {
                [NSObject showAlertMessage:@"网络请求超时"];
                
            }else if (error.code == -1009)
            {
                [NSObject showAlertMessage:@"网络请求超时"];
                
            }else
            {
                [NSObject showAlertMessage:@"服务器维护中,请稍后再试"];
            }
            failure(error);
        }
    }];
}









#pragma makr ------------- 登录 --------------
-(void)login_phone:(NSString *)phone
          password:(NSString *)password
           success:(void (^)(id responseObject,int code , NSString *message ))success
           failure:(void (^)(NSError *error))failure;
{

    NSDictionary *paramaters = @{@"accountsName":DS_NULL(phone),
                                 @"accountsPassword":DS_NULL(password)};
    
    [DSDownloadManager PostRequest:LoginCode_url parameters:paramaters success:success failure:failure];

}



@end
